package DesignPattern.SingletonPattern;

public class SingletonTest1 {
    public static void main(String[] args) {
        System.out.println(Cat.n1);
//        System.out.println(Cat.getInstance());
    }
}

//希望程序运行中只能创建一个Cat对象
class Cat{
    private String name;
    public static int n1 = 100;
    //1. 定义一个static对象
    private static Cat cat;
    //2. 构造器私有化
    private Cat(String name) {
        System.out.println("构造器被调用了");
        this.name = name;
    }
    //3. 提供一个public static方法返回一个Cat对象
    public static Cat getInstance(){
        if (cat == null){
            cat = new Cat("kitty");
        }
        return cat;
    }
}
